/**
 * 获取公选课数据
 */
var cheerio = require('cheerio');
var request = require('request');
var iconv = require('iconv-lite');
var BufferHelper = require('bufferhelper');
var DataSet = require('./dataSet.js');

function getPubLsn(cookie) {
	this._init(cookie);
}
getPubLsn.prototype = {
	_init: function(cookie) {
		this.cookie = cookie;
		this.url = 'http://210.42.121.132/stu/choose_PubLsn_list.jsp';
		this.ds = new DataSet.dataSet('PubLsn');
		this.pageCount = 1;
		this.pageNum = 1;
		this.finished = false;
	},
	_getpageCount: function(data) {
		var $ = cheerio.load(data);
		var total_count = $(".total_count a").last().attr('href').match(/pageNum=(\d{1,2})/).pop();
		this.pageCount = Number(total_count);
		console.log("RequiredLsn has " + this.pageCount);
	},
	_getData: function(page) {
		var that = this;
		var options = {
			method: 'get',
			url: this.url + '?XiaoQu=0&credit=0&keyword=&pageNum=' + page,
			headers: {
				'Cookie': this.cookie,
				'Connection': 'keep-alive'
			},
		};
		var buffer = new BufferHelper();
		request(options)
			.on('data', function(data) {
				buffer.concat(data);
			}).on('end', function() {
				var result = iconv.decode(buffer.toBuffer(), 'gb2312');
				that._analyseData(result);
				that.pageNum++;
				console.log('PubLsn page ' + page + ' complete');
				if (that.pageNum > that.pageCount) {
					that.ds.saveToFile('output/PubLsn.json');
					that.finished = true;
				}
			});
	},
	_analyseData: function(data) {
		var $ = cheerio.load(data);
		var that = this;
		var content = $('.listTable tr');
		content.each(function(index, el) {
			if (index !== 0) {
				var count = 0;
				var classDetail = {};
				$(this).children().each(function(index, el) {
					var text = $(this).text().replace(/[\r\n\t\/ ]/g, "");
					if (!text) {
						text = "NULL";
					}
					switch (count) {
						case 0:
							classDetail.className = text;
							break;
						case 1:
							classDetail.point = text;
							break;
						case 3:
							classDetail.teacher = text;
							break;
						case 4:
							classDetail.teacherStatus = text;
							break;
						case 5:
							classDetail.department = text;
							break;
						case 6:
							classDetail.book = text;
							break;
						case 7:
							classDetail.year = text;
							break;
						case 8:
							classDetail.term = text;
							break;
						case 9:
							$(this).children().each(function(index, el) {
								var text = $(this).text().replace(/[\r\n\t\/ ]/g, "");
								if (!text) {
									text = "NULL";
								}
								classDetail.timeAndPosition = text;
							});
							break;
						case 10:
							$(this).children().each(function(index, el) {
								var text = $(this).text().replace(/[\r\n\t\/ ]/g, "");
								if (!text) {
									text = "NULL";
								}
								classDetail.kind = text;
							});
							break;
						default:
							break;
					}
					count++;
				});
				that.ds.save(classDetail);
			}
		});
	},
	execute: function() {
		var that = this;
		var options = {
			method: 'get',
			url: this.url,
			headers: {
				'Cookie': this.cookie,
				'Connection': 'keep-alive'
			},
		};
		this.finished = false;
		var buffer = new BufferHelper();
		request(options)
			.on('data', function(data) {
				buffer.concat(data);
			}).on('end', function() {
				var result = iconv.decode(buffer.toBuffer(), 'gb2312');
				that._getpageCount(result);
				for (var i = 1; i <= that.pageCount; i++) {
					that._getData(i);
				}
			});
	}
};

module.exports.getPubLsn = getPubLsn;
